import router from '../../router'
const store = {
    state: {
        key: 'token',
        userKey: 'userinfo',
        token: '',
        userinfo: {}
    },
    mutations: {
        //设置key值
        setKey(state, key) {
            state.key = key
        },
        //设置userKey值
        setUserKey(state, userKey) {
            state.userKey = userKey
        },
        //存储token
        saveToken: (state, token) => {
            state.token = token
            localStorage.setItem(state.key, token)
        },
        //删除token
        clearToken: state => {
            state.token = ''
            localStorage.removeItem(state.key)
        },
        //存储用户信息
        saveUserinfo: (state, userinfo) => {
            state.userinfo = userinfo
            localStorage.setItem(state.userKey, JSON.stringify(userinfo))
        },
        //删除用户信息
        clearUserinfo: state => {
            state.userinfo = {}
            localStorage.removeItem(state.userKey)
        },
        //更新token
        refreshToken: state => {
            state.token = localStorage.getItem(state.key)
        },
        //更新userinfo
        refreshUserinfo: state => {
            state.userinfo = JSON.parse(localStorage.getItem(state.userKey))
        }
    },
    actions: {

    },
    getters: {
        //登录状态
        loginStatus: state => {
            return state.token || localStorage.getItem(state.key) ? true : false
        },
        //token
        token: state => {
            return state.token || localStorage.getItem(state.key)
        },
        //用户信息
        userinfo: state => {
            return Object.keys(state.userinfo).length ? state.userinfo : JSON.parse(localStorage.getItem(state.userKey)) || {}
        },
    }
}

export default store